using System.Runtime.Serialization;
using SqlSugar;
using Vin.Extension.Framework.Attributes;
using Vin.Extension.Framework.Entities;
using Vin.Extension.SqlSugar.Attributes;

namespace Vin.Extension.Framework.Quartz.Entities;

/// <summary>
/// 任务类型
/// </summary>
public enum JobExecuteType
{
    // 泛型
    Generics = 1,

    // 程序集
    Assembly = 2,
}

/// <summary>
/// 触发器类型
/// </summary>
public enum JobTriggerType
{
    // 简单触发器
    [Description("简单")] Simple = 1,

    // Cron表达式触发器
    [Description("Cron")] Cron = 2,
}

/// <summary>
/// 间隔类型
/// </summary>
public enum JobIntervalType
{
    // 秒
    [Description("秒")] Second = 1,

    // 分钟
    [Description("分")] Minute = 2,

    // 小时
    [Description("时")] Hour = 3,

    // 天
    [Description("天")] Day = 4
}

/// <summary>
/// 循环类型
/// </summary>
public enum JobLoopType
{
    // 一次
    [Description("执行一次")] Once = 1,

    // 有限循环
    [Description("有限循环")] Finite = 2,

    // 无限循环
    [Description("无限循环")] Infinite = 3,
}

/// <summary>
/// 任务状态
/// </summary>
[Description("StatusText")]
public enum JobStatus
{
    // 运行中
    [EnumMember(Value = "运行中")] Running = 1,

    // 暂停
    [EnumMember(Value = "已停止")] Stop = 10,

    // 已完成
    [EnumMember(Value = "已完成")] Completed = 20,
}

public class JobParams
{
    public string? Key { get; set; }

    public string? Value { get; set; }
}

[VinSqlSugarEntity("定时任务")]
public record SysJob : VinFrameworkBaseRecordEntity
{
    /// <summary>
    /// 任务名称
    /// </summary>
    [SugarColumn(ColumnDescription = "任务名称")]
    public string? Name { get; set; }

    /// <summary>
    /// 任务分组
    /// </summary>
    [SugarColumn(ColumnDescription = "任务分组")]
    public long? GroupId { get; set; }

    /// <summary>
    /// 任务执行类型 1:泛型 2:程序集
    /// </summary>
    [SugarColumn(ColumnDescription = "任务执行类型 1:泛型 2:程序集", DefaultValue = "1")]
    public JobExecuteType? ExecuteType { get; set; }

    /// <summary>
    /// 程序集路径
    /// </summary>
    [SugarColumn(ColumnDescription = "程序集路径", DefaultValue = "''")]
    public string? AssemblyPath { get; set; }

    /// <summary>
    /// 执行的程序集名称
    /// </summary>
    [SugarColumn(ColumnDescription = "执行的程序集名称", DefaultValue = "''")]
    public string? AssemblyName { get; set; }

    /// <summary>
    /// 执行的任务类名
    /// </summary>
    [SugarColumn(ColumnDescription = "执行的任务类名")]
    public string? ClassName { get; set; }

    /// <summary>
    /// 任务参数
    /// </summary>
    [SugarColumn(ColumnDescription = "任务参数", IsJson = true, ColumnDataType = "json")]
    public List<JobParams>? Parameters { get; set; }

    /// <summary>
    /// 是否立即启动一次
    /// </summary>
    [SugarColumn(ColumnDescription = "是否立即启动一次", DefaultValue = "0")]
    public bool? FirstExecute { get; set; }

    /// <summary>
    /// 开始时间
    /// </summary>
    [SugarColumn(ColumnDescription = "开始时间", DefaultValue = "0")]
    public long? StartTime { get; set; }

    /// <summary>
    /// 结束时间
    /// </summary>
    [SugarColumn(ColumnDescription = "结束时间", DefaultValue = "0")]
    public long? EndTime { get; set; }

    /// <summary>
    /// 任务触发器类型 1:简单 2:Cron
    /// </summary>
    [SugarColumn(ColumnDescription = "任务触发器类型 1:简单 2:Cron", DefaultValue = "1")]
    public JobTriggerType? TriggerType { get; set; }

    /// <summary>
    /// 循环类型
    /// </summary>
    [SugarColumn(ColumnDescription = "循环类型", DefaultValue = "1")]
    public JobLoopType? LoopType { get; set; }

    /// <summary>
    /// 循环次数
    /// </summary>
    [SugarColumn(ColumnDescription = "循环次数", DefaultValue = "-1")]
    public int? LoopCount { get; set; }

    /// <summary>
    /// 间隔类型 1:秒 2:分 3:时 4:天
    /// </summary>
    [SugarColumn(ColumnDescription = "间隔类型 1:秒 2:分 3:时 4:天", DefaultValue = "1")]
    public JobIntervalType? IntervalType { get; set; }

    /// <summary>
    /// 间隔值
    /// </summary>
    [SugarColumn(ColumnDescription = "间隔值", DefaultValue = "0")]
    public int? IntervalValue { get; set; }

    /// <summary>
    /// Cron表达式
    /// </summary>
    [SugarColumn(ColumnDescription = "Cron表达式", DefaultValue = "''")]
    public string? Cron { get; set; }

    /// <summary>
    /// 任务状态 1:运行中 10:已停止 20:已完成
    /// </summary>
    [SugarColumn(ColumnDescription = "任务状态 1:运行中 10:已停止 20:已完成", DefaultValue = "10")]
    public JobStatus? Status { get; set; }

    /// <summary>
    /// 已完成的循环次数
    /// </summary>
    [SugarColumn(ColumnDescription = "已完成的循环次数", DefaultValue = "0")]
    public int? CompletedLoopCount { get; set; }

    /// <summary>
    /// 错误次数
    /// </summary>
    [SugarColumn(ColumnDescription = "错误次数", DefaultValue = "0")]
    public int? ErrorCount { get; set; }

    /// <summary>
    /// 最后一次执行时间
    /// </summary>
    [SugarColumn(ColumnDescription = "最后一次执行时间", DefaultValue = "0")]
    public long? LastExecuteTime { get; set; }
}